#include "flashplayer.h"


FlashPlayer::FlashPlayer(QObject *parent) :
    QObject(parent)
{
//    QWebSettings::globalSettings()->setAttribute(QWebSettings::PluginsEnabled,true);

    m_playFlashCount = 0;
    m_pPlayer = new QWebView((QWidget*)parent);

    m_pPlayer->setAttribute(Qt::WA_PaintOnScreen);

//    m_pPlayer = new QWebView(0);


    m_pPlayer->settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
    m_pPlayer->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
    m_pPlayer->settings()->setAttribute(QWebSettings::AutoLoadImages, true);

    m_pPlayer->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
    m_pPlayer->settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, true);


    m_isLoadPlugin = true;

    connect(m_pPlayer, SIGNAL(loadFinished(bool)), this, SLOT(playFinish()));
//    connect(m_pPlayer, SIGNAL(loadProgress(int)), this, SLOT(flashLoadProgress(int)));
    m_pPlayer->close();
}

void FlashPlayer::setRect(QRect rect)
{
    m_pPlayer->setGeometry(rect);
    //设置可视界面的大小
    m_pPlayer->page()->setViewportSize(QSize(rect.width(), rect.height()));

}

void FlashPlayer::setRect(int ax, int ay, int aw, int ah)
{
    m_pPlayer->setGeometry(ax, ay, aw, ah);

    //设置可视界面的大小
    m_pPlayer->page()->setViewportSize(QSize(aw, ah));

//    qDebug() << "ax:" << ax << ay << aw << ah;
}

void FlashPlayer::show()
{
    m_pPlayer->show();
}

void FlashPlayer::hide()
{
    m_pPlayer->hide();
    m_pPlayer->stop();
}

void FlashPlayer::reStart()
{
    m_pPlayer->close();
    m_pPlayer->settings()->setAttribute(QWebSettings::PluginsEnabled, false);
}

void FlashPlayer::open(QString filename)
{
//    qDebug() << "=========================<<<< FlashPlayer::open >>>==============================";
    m_pPlayer->stop();
    m_playFlashCount ++;
    m_flashPath = filename;

    m_flashPath.replace("\\", "/");

    m_pPlayer->stop();

#ifdef _TTY_POSIX_
    m_flashPath = tr("%1/%2").arg(g_installPath).arg(m_flashPath);
#else
    m_flashPath = tr("%1/%2").arg(g_installPath).arg(m_flashPath);
#endif

//    qDebug() << "m_flashPath====>" << m_flashPath;
    QFile file(m_flashPath);
    if (!file.exists())
    {
        logger()->warn("Flash \"%1\" can't find\n", m_flashPath);
    }

//    g_type_init();

    //Qt在linux中，4.7之前的版本QWebView一旦加载flash,程序马上就会段错误
#ifdef _TTY_POSIX_
    if (QT_VERSION > 0x040604)
    {
#ifdef SLCD_DEBUG
        qDebug() << "QT_Verison is biger than 4.7, current version is" << QT_VERSION_STR;
        m_pPlayer->load(m_flashPath);
#endif
    } else {
#ifdef SLCD_DEBUG
        m_pPlayer->load(QString(""));
        qDebug() << "QT_Verison is less than 4.7, current version is" << QT_VERSION_STR
                 << "load flash will err, so this version will not load flash";
#endif
    }
#else
    m_pPlayer->load(m_flashPath);
#endif

//    m_pPlayer->load(QUrl::fromUserInput("http://www.baidu.com/"));

}

void FlashPlayer::flashLoadProgress(const int progress)
{

//    qDebug() << m_playFlashCount << "flash flashLoadProgress===>" << progress;
}

void FlashPlayer::playFinish()
{
//    qDebug() << ">>>>>>>>>>>>> FlashPlayer::playFinish() <<<<<<<<<<<<<<<";
    emit msgFinish();
}
